(function() {
    'use strict';

    /**
     * @ngdoc function
     * @name webApp.controller:KanjiaCtrl
     * @description
     * # KanjiaCtrl
     * Controller of the webApp
     */
    angular.module('webApp')
        .controller('KanjiaCtrl', KanjiaCtrl);

    KanjiaCtrl.$inject = ['$scope', 'dataService', 'helper', '$rootScope', '$location', 'sessionService'];

    function KanjiaCtrl($scope, dataService, helper, $rootScope, $location, sessionService) {

        //打折活动列表
        $scope.listAction = function() {
            $scope.pageSize = 10;
            $scope.stype = "KANR";
            $scope.getList(1);
            $scope.$on('event:up-store-id', function() {
                // $scope.store_id = $rootScope.store_id
                 $scope.getList(1);
            });
        }

        $scope.getList = function(pageNo) {
            var user = sessionService.get("tokenUser");

            var query = {
                page_no: pageNo ? pageNo : "1",
                page_size: $scope.pageSize,
                order_by: "bargain_rule_id:desc",
            };
            query.query = { "partner_id": user.CompanyId };

            // console.error("query--->", query);

            dataService.kanjiaGet(query).then(function(list) {
                if (list) {
                    for (var i in list.bargain_rules) {
                        list.bargain_rules[i].diff_time = $scope.diff_time(list.bargain_rules[i].start_time, list.bargain_rules[i].end_time)
                    }
                    $scope.bargain_rules_list = list.bargain_rules;
                    $scope.bargain_rules_total = list.total_count;
                }
            });
        }

        //js计算两个时间的时间差
        $scope.diff_time = function(start_time, end_time) {
            var s_time = new Date().getTime();
            var start = new Date(start_time).getTime();
            var end = new Date(end_time).getTime();

            //当前时间是否大于活动结束时间
            if (s_time > end) {
                return 0;
            }

            //判断当前时间是否大于开始时间
            if (s_time > start && s_time < end) {
                var time = new Date().getTime();
            } else {
                var time = new Date(start_time).getTime();
            }

            //var diff = end_time * 1000 - start_time * 1000 //时间差的毫秒数  
            var diff = end - time;

            //计算出相差天数  
            var days = Math.floor(diff / (24 * 3600 * 1000));

            //计算出小时数  
            var leave1 = diff % (24 * 3600 * 1000); //计算天数后剩余的毫秒数  
            var hours = Math.floor(leave1 / (3600 * 1000));
            //计算相差分钟数  
            var leave2 = leave1 % (3600 * 1000); //计算小时数后剩余的毫秒数  
            var minutes = Math.floor(leave2 / (60 * 1000));

            //计算相差秒数  
            var leave3 = leave2 % (60 * 1000); //计算分钟数后剩余的毫秒数  
            var seconds = Math.round(leave3 / 1000);

            var returnStr = seconds + "秒";
            if (minutes > 0) {
                returnStr = minutes + "分" + returnStr;
            }
            if (hours > 0) {
                returnStr = hours + "小时" + returnStr;
            }
            if (days > 0) {
                returnStr = days + "天" + returnStr;
            }
            return returnStr;
        }

        //砍价添加页面
        $scope.addAction = function() {
            var bargain_rule_id = $location.search().bargain_rule_id;

            // console.error("bargain_rule_id--->", bargain_rule_id);
            // return false;

            if (bargain_rule_id) {
                var query = { "query": { "bargain_rule_id": bargain_rule_id } };

                dataService.kanjiaGetOne(query).then(function(result) {
                    console.error("砍价数据--->", result);

                    $scope.kanjia_data = result;
                    $scope.kanjia_data.store_id = $rootScope.store_id;
                    $scope.bargain_rule_id = result.bargain_rule_id;

                    var item_id = result.item_id;
                    var sku_id = result.sku_id;
                    // console.error("item_id-->", item_id, "|| sku_id-->", sku_id);
                    dataService.itemSkuOne(item_id, sku_id).then(function(item_data) {
                        // console.error("选中商品-->", item_data);
                        var a = clone(item_data);
                        a.sku_data = item_data.sku;
                        $scope.checked_item = a;
                    });
                });
            } else {
                var data = {
                    "store_id": $rootScope.store_id,
                    "title": "砍价促销",
                    "need_bargain_times": 10, //总需要拉人砍多少次
                    "need_cut_off_fee": 1, //总共砍掉多少元
                    "start_time": $scope.getDateStr(0),
                    "end_time": $scope.getDateStr(7),
                    "cut_way": 1, //砍价方式 1=均价砍 2=随机砍
                    "min_bargain_times": 0, //使用优惠的最低砍价次数
                };
                $scope.kanjia_data = data;
            }
        }

        //砍价添加页面
        $scope.infoAction = function() {
            $scope.bargain_rule_id = $location.search().bargain_rule_id;
            $scope.stype = "KANJ";
            $scope.ffectget();
            $scope.infoGet(1);
        }

        $scope.ffectget = function() {
            var data = { bargain_rule_id: $scope.bargain_rule_id }
            dataService.ffectget(data).then(function(data) {
                $scope.total_bargain = data;
                // $scope.total_bargain.total_bargain_activity = 21;
            });
        }

        $scope.infoGet = function(pageNo) {
            var data = { page_no: pageNo ? pageNo : "1", page_size: $scope.page_Size, query: { bargain_rule_id: $scope.bargain_rule_id }, order_by: "total_bargain_times:desc" }
            dataService.activityget(data).then(function(data) {
                $scope.bargain_activitys = data.bargain_activitys;
                $scope.total_count = data.total_count;
            });
        }

        //打折活动添加
        $scope.kanjiaAdd = function() {
            if ($scope.kanjia_data.title.length > 5) {
                helper.myAlert("error:砍价标题不能超过5个字");
                return false;
            }

            if ($scope.kanjia_data.end_time < $scope.kanjia_data.start_time) {
                helper.myAlert("error:砍价活动结束时间不能小于活动开始时间");
                return false;
            }

            var bargain_rule_id = $location.search().bargain_rule_id;
            if (bargain_rule_id) {
                dataService.kanjiaUpdate($scope.kanjia_data).then(function(result) {
                    if (result.bargain_rule_id) {
                        helper.myAlert("success", "#/kanjia/list");
                    }
                });
            } else {
                console.log("砍价data--->", $scope.kanjia_data);
                // return false;
                dataService.kanjiaAdd($scope.kanjia_data).then(function(result) {
                    console.error("砍价返回结果：", result);

                    if (result.bargain_rule_id) {
                        $scope.bargain_rule_id = result.bargain_rule_id;
                        helper.myAlert("success", "#/kanjia/list");
                    }
                });
            }
        }

        //砍价活动暂停
        $scope.kanjiaPause = function(bargain_rule_id) {
            var query = { "query": { "bargain_rule_id": bargain_rule_id } };

            dataService.kanjiaGetOne(query).then(function(result) {
                $scope.kanjia_data = result;
                $scope.kanjia_data.end_time = $scope.getDateStr(0);

                dataService.kanjiaUpdate($scope.kanjia_data).then(function(result) {
                    if (result.bargain_rule_id) {
                        $scope.getList();
                    }
                });
            });
        }

        //砍价活动删除
        $scope.kanjiaDelete = function(bargain_rule_id) {
            helper.myConfirm('确认删除吗？', function() {
                var data = { 'bargain_rule_id': bargain_rule_id };
                console.error("删除--->", data);
                // return false;
                dataService.kanjiaDelete(data).then(function(result) {
                    console.error("删除结果：", result);
                    // return false;
                    if (result.bargain_rule_id) {
                        $scope.getList(1);
                    } else {
                        helper.myAlert("error:" + result);
                    }
                });
            });
        }

        //活动时间计算
        $scope.getDateStr = function(addDayCount) {
            var dd = new Date();
            dd.setDate(dd.getDate() + addDayCount);
            var y = dd.getFullYear();
            var m = (dd.getMonth() + 1) < 10 ? "0" + (dd.getMonth() + 1) : (dd.getMonth() + 1);
            var d = dd.getDate() < 10 ? "0" + dd.getDate() : dd.getDate();
            var h = dd.getHours();
            var i = dd.getMinutes();
            var s = dd.getSeconds();

            if (i < 10) {
                i = "0" + i;
            }
            var s = dd.getSeconds();
            if (s < 10) {
                s = "0" + s;
            }

            return y + "-" + m + "-" + d + " " + h + ":" + i + ":" + s;
        }

        //商品directives回调方法
        $scope.itemdata = function(items) {
            //商品信息是多维数组时
            if (items) {
                //选中商品的item_id sku_id
                $scope.kanjia_data.item_id = items.sku_data.item_id;
                $scope.kanjia_data.sku_id = items.sku_data.sku_id;

                //选中商品
                $scope.checked_item = items;

                console.error("砍价数据--->", $scope.kanjia_data);
            }

        }
    }
})();